#!/bin/bash

clear
echo "*** Matty's Checkm8 APNonce Setter ***"
echo "你是输入NONCE值，还是选取shsh文件进行generator值的固定? (y：输入；n：选取文件)"

read input

if [ $input = y ];
then
    echo "请输入所需的generator值.(例如：0x1111111111111111)"

    read generator

    echo "你输入的generator值是$generator"
elif [ $input = n ];
then

    echo "请将要降级的SHSH文件拖放到此终端窗口中，然后按enter键"

    read shsh

    echo "$shsh是shsh的正确位置和文件名吗？(输入y或者n)"

    read pass

        if [ $pass == yes ] || [ $pass == Yes ] || [ $pass == y ] || [ $pass == Y ];
        then
            echo "继续指定的SHSH"

        elif [ $pass == no ] || [ $pass == No ] || [ $pass == n ] || [ $pass == n ];
        then
            echo "请重新启动脚本并提供正确的位置和文件名"
            echo "正在退出..."
            exit

        else
            echo "无法识别的输入"
            echo "正在退出..."
            exit

        fi

        if [ ${shsh: -6} == ".shsh2" ] || [ ${shsh: -5} == ".shsh" ];
        then
            echo "文件已验证为SHSH2文件，请继续"

        else
            echo "请确保文件扩展名为.shsh或者.shsh2，并重新开始"
            echo "正在退出..."
            exit
        fi

        echo "从SHSH获取generator数值以继续"

        getGenerator() {
        echo $1 | grep "<string>0x" $shsh  | cut -c10-27
        }
        generator=$(getGenerator $shsh)

        if [ -z "$generator" ]
        then
            echo "[错误] SHSH不包含generator数值!"
            echo "[错误] 请使用带有generator值的不同SHSH!"
            echo "[错误] 使用 shsh.host或tsssaver.1conan.com保存的 SHSH 是可以接受的"
            echo "正在退出..."
            exit
        else
            echo "你的generator值是: $generator"
        fi

else
    echo "输入无法识别，正在退出..."
    exit
fi

echo "$generator"
echo "正在获取设备型号..."

files/igetnonce | grep 'n69ap' &> /dev/null
if [ $? == 0 ]; then
   echo "是支持的设备"
   device="n69ap"
   echo $device
fi

files/igetnonce | grep 'n69uap' &> /dev/null
if [ $? == 0 ]; then
   echo "是支持的设备"
   device="n69uap"
   echo $device
fi

files/igetnonce | grep 'n71ap' &> /dev/null
if [ $? == 0 ]; then
   echo "是支持的设备"
   device="n71ap"
   echo $device
fi

files/igetnonce | grep 'n71map' &> /dev/null
if [ $? == 0 ]; then
   echo "是支持的设备"
   device="n71map"
   echo $device
fi
files/igetnonce | grep 'n66ap' &> /dev/null
if [ $? == 0 ]; then
   echo "是支持的设备"
   device="n71map"
   echo $device
fi
files/igetnonce | grep 'n66map' &> /dev/null
if [ $? == 0 ]; then
   echo "是支持的设备"
   device="n71map"
   echo $device
fi


if [ -z "$device" ]
then
    echo "不支持的设备或未找到设备。"
    echo "正在退出.."
    exit
else
    echo "找到支持的设备。"
fi

echo "请在DFU模式下连接设备。准备好继续时按回车"
echo ""
echo "期间需要多次拔插设备，直到出现：Now you can boot untrusted images."
echo ""

read randomIrrelevant

echo "正在获取设备的芯片类型..."

cd files

if [ $device == n69ap ] || [ $device == n71map ] || [ $device == n66map ]; then
    echo "设备芯片为高通芯片，使用eclipsa8003"
    ./eclipsa8003
    sleep 1
else
    echo "设备芯片为三星芯片，使用eclipsa8000"
    ./eclipsa8000
    sleep 1
fi
echo "设备现在处于已删除签名检查的pwned DFU模式（感谢 0x7ff）"

sleep 2

echo "上传引导链文件中..."

iBSS="iBSS.$device.img4"
iBEC="iBEC.$device.img4"
echo "正在上传 $iBSS"
./irecovery -f $iBSS
sleep 2
echo "正在上传 $iBEC"
./irecovery -f $iBEC
sleep 2
echo " "
echo "当前设备现在应该处于pwned的恢复模式"
echo " "
echo "当前的nonce值"
./irecovery -q | grep NONC
echo "正在将nonce值设置为 $generator"
./irecovery -c "bgcolor 255 0 0"
sleep 1
./irecovery -c "setenv com.apple.System.boot-nonce $generator"
sleep 1
./irecovery -c "saveenv"
sleep 1
./irecovery -c "setenv auto-boot false"
sleep 1
./irecovery -c "saveenv"
sleep 1
./irecovery -c "reset"
echo "等待设备重新启动进入恢复模式"
sleep 7
echo " "
echo " "
echo "你设定的nonce值"
./irecovery -q | grep NONC

echo "nonce值已设定完成!"
echo ""
echo "如果SEP和Baseband与当前此SHSH对其有效的固件是兼容的，您现在可以使用futurerestore恢复到此SHSH对其有效的固件"
echo " "
